#include "toolbar.h"
#include "qglobal.h"
#include "global/global.h"
#include "utils/xatom-helper.h"
#include <QMenu>
#include <QToolTip>

ToolBar::ToolBar(QWidget *parent) : QWidget(parent)
{
    m_styleSettings = new QGSettings("org.ukui.style");
    this->setAttribute(Qt::WA_AlwaysShowToolTips);//焦点不在窗口上时鼠标悬浮可显示tooltips
    this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
    this->setAttribute(Qt::WA_TranslucentBackground);
//    this->setFocusPolicy(Qt::NoFocus);

    this->initLayout();
    this->initMenu();
    this->platformType();
    this->initGsetting();
    this->initControlQss();
    this->initConnect();
    this->setBtnIcon();
    this->setBtnIconSize(TOOL_BUTTON_ICON);
    this->resumeStatus();
    this->setFont();
    this->setCursor(Qt::ArrowCursor);
}

ToolBar::~ToolBar()
{
}

void ToolBar::initLayout()
{
    //X11窗口装饰
    MotifWmHints hints1;
    hints1.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
    hints1.functions = MWM_FUNC_ALL;
    hints1.decorations = MWM_DECOR_BORDER;
    XAtomHelper::getInstance()->setWindowMotifHint(winId(), hints1);

    //布局
    g_tooleWid = new QFrame(this);
    m_toolLayout = new QHBoxLayout(this);

    m_cursorWidget = new QWidget(this);
    m_cursorWidget->setAttribute(Qt::WA_TranslucentBackground);
    m_soundWidget = new QWidget(this);
    m_soundWidget->setAttribute(Qt::WA_TranslucentBackground);
    m_keyWidget = new QWidget(this);
    m_keyWidget->setAttribute(Qt::WA_TranslucentBackground);
    m_timeWidget = new QWidget(this);
    m_timeWidget->setAttribute(Qt::WA_TranslucentBackground);
    m_settingWidget = new QWidget(this);
    m_settingWidget->setAttribute(Qt::WA_TranslucentBackground);
    m_closeWidget = new QWidget(this);
    m_closeWidget->setAttribute(Qt::WA_TranslucentBackground);
    m_startWidget = new QWidget(this);
    m_startWidget->setAttribute(Qt::WA_TranslucentBackground);

    m_cursorWidget->setFixedWidth(TOOL_BUTTON_WIDTH);
    m_cursorVLayout = new QVBoxLayout(m_cursorWidget);
    m_cursorVLayout->setContentsMargins(0,5,0,0);
    m_cursorHLayout = new QHBoxLayout();
    m_cursorrec = new KPressButton(this);
    m_cursorrec->setFocusPolicy(Qt::NoFocus);
    m_cursorrec->setAttribute(Qt::WA_TranslucentBackground);
    m_cursorrec->setChecked(m_isCursorRec);
    m_cursorrec->setStyleSheet("KPressButton{border:0px;background:transparent; background-color:rgba(46, 140, 255, 255)}"
                                "KPressButton::hover{border:0px;background:transparent; background-color:rgba(52, 137, 246, 255)}"
                                "KPressButton::pressed{border:0px;background:transparent; background-color:rgba(51, 116, 200, 255)}");
    m_cursorlabel = new QLabel(this);
    m_cursorlabel->setFixedSize(TOOL_Lable_Nav);
    m_cursorlabel->setAttribute(Qt::WA_TranslucentBackground);
    m_cursorrecNav = new kdk::KBorderlessButton(this);
    m_cursorrecNav->setFixedSize(TOOL_BUTTON_Nav);
    m_cursorHLayout->addWidget(m_cursorlabel, 0, Qt::AlignLeft);
    m_cursorHLayout->addWidget(m_cursorrecNav);
    m_cursorVLayout->addWidget(m_cursorrec, 0, Qt::AlignCenter);
    m_cursorVLayout->addSpacing(BUTTON_LABEL_SPACING);
    m_cursorVLayout->addLayout(m_cursorHLayout);

    m_soundWidget->setFixedWidth(TOOL_BUTTON_WIDTH);
    m_soundVLayout = new QVBoxLayout(m_soundWidget);
    m_soundVLayout->setContentsMargins(0,5,0,0);
    m_soundHLayout = new QHBoxLayout();
    m_soundrec = new KPressButton(this);
    m_soundrec->setFocusPolicy(Qt::NoFocus);
    m_soundrec->setAttribute(Qt::WA_TranslucentBackground);
    m_soundrec->setChecked(m_isOpenSound);
    m_soundrec->setStyleSheet("KPressButton{border:0px;background:transparent; background-color:rgba(46, 140, 255, 255)}"
                                "KPressButton::hover{border:0px;background:transparent; background-color:rgba(52, 137, 246, 255)}"
                                "KPressButton::pressed{border:0px;background:transparent; background-color:rgba(51, 116, 200, 255)}");
    m_soundlabel = new QLabel(this);
    m_soundlabel->setAttribute(Qt::WA_TranslucentBackground);
    m_soundlabel->setFixedSize(TOOL_Lable_Nav);
    m_soundrecNav = new kdk::KBorderlessButton(this);
    m_soundrecNav->setFixedSize(TOOL_BUTTON_Nav);
    m_soundHLayout->addWidget(m_soundlabel);
    m_soundHLayout->addWidget(m_soundrecNav);
    m_soundVLayout->addWidget(m_soundrec, 0, Qt::AlignCenter);
    m_soundVLayout->addSpacing(BUTTON_LABEL_SPACING);
    m_soundVLayout->addLayout(m_soundHLayout);

    m_keyWidget->setFixedWidth(TOOL_BUTTON_WIDTH);
    m_keyVLayout = new QVBoxLayout(m_keyWidget);
    m_keyVLayout->setContentsMargins(0,5,0,0);
    m_keyrec = new KPressButton(this);
    m_keyrec->setFocusPolicy(Qt::NoFocus);
    m_keyrec->setAttribute(Qt::WA_TranslucentBackground);
    m_keyrec->setStyleSheet("KPressButton{border:0px;background:transparent;}"
                                "KPressButton::hover{border:0px;background:transparent; background-color:rgba(196, 203, 206, 255)}"
                                "KPressButton::pressed{border:0px;background:transparent; background-color:rgba(192, 197, 195, 255)}");
    m_keylabel = new QLabel(m_keyWidget);
    m_keylabel->setAttribute(Qt::WA_TranslucentBackground);
    m_keylabel->setFixedSize(TOOL_Lable_Nav);
    m_keyVLayout->addWidget(m_keyrec, 0, Qt::AlignCenter);
    m_keyVLayout->addSpacing(BUTTON_LABEL_SPACING);
    m_keyVLayout->addWidget(m_keylabel, 0, Qt::AlignCenter);

    m_timeWidget->setFixedWidth(TOOL_BUTTON_WIDTH-18);
    m_timeVLayout = new QVBoxLayout(m_timeWidget);
    m_timeVLayout->setContentsMargins(0,5,0,0);
    m_timeHLayout = new QHBoxLayout();
    m_time = new KPushButton(this);
    m_time->setFocusPolicy(Qt::NoFocus);
    m_time->setStyleSheet("KPressButton{border:0px;background:transparent;}"
                                "KPressButton::hover{border:0px;background:transparent; background-color:rgba(196, 203, 206, 255)}"
                                "KPressButton::pressed{border:0px;background:transparent; background-color:rgba(192, 197, 195, 255)}");
    m_time->setAttribute(Qt::WA_TranslucentBackground);
    m_timelabel = new QLabel(m_timeWidget);
    m_timelabel->setAttribute(Qt::WA_TranslucentBackground);
    m_timelabel->setFixedSize(TOOL_Lable_Nav1);
    m_timeNav = new kdk::KBorderlessButton(this);
    m_timeNav->setFixedSize(TOOL_BUTTON_Nav);
    m_timeHLayout->addWidget(m_timelabel, 0, Qt::AlignRight);
    m_timeHLayout->addWidget(m_timeNav, 0, Qt::AlignBottom);
    m_timeVLayout->addWidget(m_time, 0, Qt::AlignCenter);
    m_timeVLayout->addSpacing(BUTTON_LABEL_SPACING);
    m_timeVLayout->addLayout(m_timeHLayout);

    m_line1 = new QLabel(this);
    m_line1->setFixedSize(1, 24);

    m_settingVLayout = new QVBoxLayout(m_settingWidget);
    m_settingVLayout->setContentsMargins(0,5,0,0);
    m_setting = new KPushButton(this);
    m_setting->setFocusPolicy(Qt::NoFocus);
    m_setting->setAttribute(Qt::WA_TranslucentBackground);
    m_setting->setStyleSheet("KPushButton{border:0px;background:transparent;}"
                                "KPushButton::hover{border:0px;background:transparent; background-color:rgba(196, 203, 206, 255)}"
                                "KPushButton::pressed{border:0px;background:transparent; background-color:rgba(192, 197, 195, 255)}");
    m_settinglabel = new QLabel(m_settingWidget);
    m_settinglabel->setAttribute(Qt::WA_TranslucentBackground);
    m_settinglabel->setFixedSize(TOOL_Lable_Nav2);
    m_settingVLayout->addWidget(m_setting, 0, Qt::AlignHCenter);
    m_settingVLayout->addSpacing(BUTTON_LABEL_SPACING);
    m_settingVLayout->addWidget(m_settinglabel, 0, Qt::AlignHCenter);

    m_closeVLayout = new QVBoxLayout(m_closeWidget);
    m_closeVLayout->setContentsMargins(0,5,0,0);
    m_close = new KPushButton(this);
    m_close->setFocusPolicy(Qt::NoFocus);
    m_close->setAttribute(Qt::WA_TranslucentBackground);
    m_close->setStyleSheet("KPushButton{border:0px;background:transparent;}"
                                "KPushButton::hover{border:0px;background:transparent; background-color:rgba(196, 203, 206, 255)}"
                                "KPushButton::pressed{border:0px;background:transparent; background-color:rgba(192, 197, 195, 255)}");
    m_closelabel = new QLabel(m_closeWidget);
    m_closelabel->setAttribute(Qt::WA_TranslucentBackground);
    m_closelabel->setFixedSize(TOOL_Lable_Nav2);
    m_closeVLayout->addWidget(m_close, 0, Qt::AlignCenter);
    m_closeVLayout->addSpacing(BUTTON_LABEL_SPACING);
    m_closeVLayout->addWidget(m_closelabel, 0, Qt::AlignCenter);

    m_startVLayout = new QVBoxLayout(m_startWidget);
    m_startVLayout->setContentsMargins(0,5,0,0);
    m_start = new KPushButton(this);
    m_start->setFocusPolicy(Qt::NoFocus);
    m_start->setStyleSheet("KPushButton{border:0px;background:transparent;background-image: url(:/icon/start_rec.svg);}"
                                "KPushButton::hover{border:0px;background:transparent; background-image: url(:/icon/start-hover.svg);}"
                                "KPushButton::pressed{border:0px;background:transparent; background-image: url(:/icon/start-click.svg);}");
    m_startlabel = new QLabel(m_closeWidget);
    m_startlabel->setAttribute(Qt::WA_TranslucentBackground);
    m_startlabel->setFixedSize(TOOL_Lable_Nav2);
    m_start->setAttribute(Qt::WA_TranslucentBackground);
    m_startVLayout->addWidget(m_start, 0, Qt::AlignCenter);
    m_startVLayout->addWidget(m_startlabel, 0, Qt::AlignCenter);

    //绘制阴影
    QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect(this);
    effect->setOffset(0, 0); //设置向哪个方向产生阴影效果(dx,dy)，特别地，(0,0)代表向四周发散
    effect->setColor(TOOL_COLOR);       //设置阴影颜色，也可以setColor(QColor(220,220,220))
    effect->setBlurRadius(BLUR_RADIUS); //设定阴影的模糊半径，数值越大越模糊
    g_tooleWid->setGraphicsEffect(effect);
}

void ToolBar::initMenu()
{
    m_cursorrecMenu = new QMenu(this);
    m_actShowMouse = new QAction(m_cursorrecMenu);
    m_actShowMouse->setCheckable(true);
    m_actShowMouse->setText(tr("show cursor"));
    m_actShowMovePath = new QAction(m_cursorrecMenu);
    m_actShowMovePath->setCheckable(true);
    m_actShowMovePath->setText(tr("move path"));
    m_actClik = new QAction(m_cursorrecMenu);
    m_actClik->setCheckable(true);
    m_actClik->setText(tr("click effect"));
    m_cursorrecMenu->addAction(m_actShowMouse);
    m_cursorrecMenu->addAction(m_actShowMovePath);
    m_cursorrecMenu->addAction(m_actClik);

    m_soundrecMenu = new QMenu(this);
    m_actMic = new QAction(m_soundrecMenu);
    m_actMic->setCheckable(true);
    m_actMic->setText(tr("mic"));
    m_actSysSound = new QAction(m_soundrecMenu);
    m_actSysSound->setCheckable(true);
    m_actSysSound->setChecked(true);
    m_actSysSound->setText(tr("sys sound"));
    m_soundrecMenu->addAction(m_actMic);
    m_soundrecMenu->addAction(m_actSysSound);

    m_timeMenu = new QMenu(this);
    m_timeMenu->setFixedSize(100, 163);
    m_actNone = new QAction(m_timeMenu);
    m_actNone->setText(tr("none"));
    m_actThree = new QAction(m_timeMenu);
    m_actThree->setText(tr("3s"));
    m_actFive = new QAction(m_timeMenu);
    m_actFive->setText(tr("5s"));
    m_actTen = new QAction(m_timeMenu);
    m_actTen->setText(tr("10s"));
    m_timeMenu->addAction(m_actNone);
    m_timeMenu->addAction(m_actThree);
    m_timeMenu->addAction(m_actFive);
    m_timeMenu->addAction(m_actTen);
    QString locale = QLocale::system().name();
    if (locale == "zh_HK" || locale == "zh_CN") {
        m_cursorrecMenu->setFixedSize(TOOL_MENU_WIDTH, 128);
        m_soundrecMenu->setFixedSize(TOOL_MENU_WIDTH, 90);
        if (m_info.waylandDectected())
        {
            m_actShowMovePath->setVisible(false);
            m_actClik->setVisible(false);
            m_cursorrecMenu->setFixedSize(TOOL_MENU_WIDTH, 48);
        }
    } else {
        m_cursorrecMenu->setFixedHeight(128);
        m_soundrecMenu->setFixedHeight(90);
        if (m_info.waylandDectected())
        {
            m_actShowMovePath->setVisible(false);
            m_actClik->setVisible(false);
            m_cursorrecMenu->setFixedHeight(48);
        }
    }
}

void ToolBar::initControlQss()
{
//    if (Global::platForm.contains(Variable::platForm)) {
        m_toolLayout->addWidget(m_cursorWidget);
        m_toolLayout->addWidget(m_soundWidget);
        m_toolLayout->addWidget(m_keyWidget);
        m_toolLayout->addWidget(m_timeWidget);
        m_toolLayout->setSpacing(8);
        m_toolLayout->addWidget(m_line1, 0, Qt::AlignVCenter);
        m_toolLayout->setSpacing(8);
        m_toolLayout->addWidget(m_settingWidget);
        m_toolLayout->addWidget(m_closeWidget);
        m_toolLayout->addWidget(m_startWidget);

//        m_toolLayout->setContentsMargins(10, 0, 10, 0);
        g_tooleWid->setLayout(m_toolLayout);
        g_tooleWid->move(2, 2);
        g_tooleWid->resize(this->width() - 4, this->height() - 4);
//    } else {
        //    }
}

bool ToolBar::event(QEvent *event)
{
    if (event->type() == QEvent::ToolTip){
        QHelpEvent *helpEvent = static_cast<QHelpEvent*>(event);
        QPoint pShow;
        int index = itemAt(helpEvent->pos(), pShow);
        if (index != -1)
        {
            QToolTip::showText(pShow, getTooltip(index));
        }
        else
        {
            QToolTip::hideText();
            event->ignore();
        }
        return true;
    }

    return QWidget::event(event);
}

int ToolBar::itemAt(const QPoint &p, QPoint &pShow)
{
    QPoint point1 = m_cursorrec->mapTo(g_tooleWid, QPoint(0, 0));
    QPoint point2 = m_soundrec->mapTo(g_tooleWid, QPoint(0, 0));
    QPoint point3 = m_keyrec->mapTo(g_tooleWid, QPoint(0, 0));
    QPoint point4 = m_time->mapTo(g_tooleWid, QPoint(0, 0));
    QPoint point5 = m_setting->mapTo(g_tooleWid, QPoint(0, 0));
    QPoint point6 = m_close->mapTo(g_tooleWid, QPoint(0, 0));
    QPoint point7 = m_start->mapTo(g_tooleWid, QPoint(0, 0));
    if ((point1.x() < p.x()) && (point1.y() < p.y()) && (point1.x() + 36 > p.x()) && (point1.y() + 36 > p.y()))
    {
        pShow = m_cursorrec->mapToGlobal(QPoint(0, 0));
        pShow.setX(pShow.x() - 50);
        pShow.setY(pShow.y() - 75);
        return 1;
    }
    else if ((point2.x() < p.x()) && (point2.y() < p.y()) && (point2.x() + 36 > p.x()) && (point2.y() + 36 > p.y()))
    {
        pShow = m_soundrec->mapToGlobal(QPoint(0, 0));
        pShow.setX(pShow.x() - 50);
        pShow.setY(pShow.y() - 75);
        return 2;
    }
    else if ((point3.x() < p.x()) && (point3.y() < p.y()) && (point3.x() + 36 > p.x()) && (point3.y() + 36 > p.y()))
    {
        pShow = m_keyrec->mapToGlobal(QPoint(0, 0));
        pShow.setX(pShow.x() - 50);
        pShow.setY(pShow.y() - 75);
        return 3;
    }
    else if ((point4.x() < p.x()) && (point4.y() < p.y()) && (point4.x() + 36 > p.x()) && (point4.y() + 36 > p.y()))
    {
        pShow = m_time->mapToGlobal(QPoint(0, 0));
        pShow.setX(pShow.x() - 50);
        pShow.setY(pShow.y() - 75);
        return 4;
    }
    else if ((point5.x() < p.x()) && (point5.y() < p.y()) && (point5.x() + 36 > p.x()) && (point5.y() + 36 > p.y()))
    {
        pShow = m_setting->mapToGlobal(QPoint(0, 0));
        pShow.setX(pShow.x() - 40);
        pShow.setY(pShow.y() - 75);
        return 5;
    }
    else if ((point6.x() < p.x()) && (point6.y() < p.y()) && (point6.x() + 36 > p.x()) && (point6.y() + 36 > p.y()))
    {
        pShow = m_close->mapToGlobal(QPoint(0, 0));
        pShow.setX(pShow.x() - 40);
        pShow.setY(pShow.y() - 75);
        return 6;
    }
    else if ((point7.x() < p.x()) && (point7.y() < p.y()) && (point7.x() + 36 > p.x()) && (point7.y() + 36 > p.y()))
    {
        pShow = m_start->mapToGlobal(QPoint(0, 0));
        pShow.setX(pShow.x() - 20);
        pShow.setY(pShow.y() - 75);
        return 7;
    }

    return -1;
}

QString ToolBar::getTooltip(const int &index)
{
    QString sResult;
    switch (index) {
    case 1 : sResult = m_isCursorRec ? tr("click close cursor recording") : tr("click open cursor recording"); break;
    case 2 : sResult = m_isOpenSound ? tr("click close voice recording") : tr("click open voice recording"); break;
    case 3 : sResult = m_isKeyRec ? tr("click close key press recording") : tr("click open key press recording"); break;
    case 4 : sResult = tr("click switch countdown"); break;
    case 5 : sResult = tr("click open setting"); break;
    case 6 : sResult = tr("click close screen recording"); break;
    case 7 : sResult = tr("start screen recording"); break;
    default: sResult = ""; break;
    }

    return sResult;
}

void ToolBar::soundrecNav()
{    
    m_soundrecNav->setIcon(QIcon(":/icon/uparrow.svg"));
    QPoint menuPos = m_soundrecNav->mapToGlobal(QPoint(0, 0)); // 获取m_edit在全局坐标系中的位置
    QSize menuSize = m_soundrecMenu->sizeHint();
    menuPos.setX(menuPos.x() - ((menuSize.width() - m_soundrecNav->width()) / 2));
    menuPos.setY(menuPos.y() + 35);
    m_soundrecMenu->exec(menuPos); // 在计算的位置显示菜单
}

void ToolBar::time()
{
    //数据埋点 - 倒计时录制
    KTrackData *node = kdk_dia_data_init(KEVENTSOURCE_DESKTOP, KEVENT_CLICK);
    kdk_dia_upload_default(node, "enable-countcut-record", "ToolBar");
    kdk_dia_data_free(node);

    m_time->setChecked(false);
    timeNav();
}

void ToolBar::timeNav()
{
    m_timeNav->setIcon(QIcon(":/icon/uparrow.svg"));
    QPoint menuPos = m_timeNav->mapToGlobal(QPoint(0, 0)); // 获取m_edit在全局坐标系中的位置
    QSize menuSize = m_timeMenu->sizeHint();
    menuPos.setX(menuPos.x() - ((menuSize.width() - m_timeNav->width()) / 2));
    menuPos.setY(menuPos.y() + 35);
    m_timeMenu->exec(menuPos); // 在计算的位置显示菜单
}

void ToolBar::timeNone()
{
    m_time->setText(tr("none"));
    m_countdown = 0;
    m_Data->set("cutdown", m_countdown);
}

void ToolBar::timeThree()
{
    m_time->setText(tr("3s"));
    m_countdown = 3;
    m_Data->set("cutdown", m_countdown);
}

void ToolBar::timeFive()
{
    m_time->setText(tr("5s"));
    m_countdown = 5;
    m_Data->set("cutdown", m_countdown);
}

void ToolBar::timeTen()
{
    m_time->setText(tr("10s"));
    m_countdown = 10;
    m_Data->set("cutdown", m_countdown);
}

void ToolBar::soundMic()
{
    KTrackData *node = kdk_dia_data_init(KEVENTSOURCE_DESKTOP, KEVENT_CLICK);
    kdk_dia_upload_default(node, "enable-sound-mic", "ToolBar");
    kdk_dia_data_free(node);

    m_isOpenMic = !m_isOpenMic;
    m_actMic->setChecked(m_isOpenMic);
    m_isOpenSound = m_isOpenSysSound || m_isOpenMic;
    m_soundrec->setChecked(m_isOpenSound);

    m_Data->set("openmic", m_actMic->isChecked());
}

void ToolBar::soundSys()
{
    KTrackData *node = kdk_dia_data_init(KEVENTSOURCE_DESKTOP, KEVENT_CLICK);
    kdk_dia_upload_default(node, "enable-sound-system", "ToolBar");
    kdk_dia_data_free(node);

    m_isOpenSysSound = !m_isOpenSysSound;
    m_actSysSound->setChecked(m_isOpenSysSound);
    m_isOpenSound = m_isOpenSysSound || m_isOpenMic;
    m_soundrec->setChecked(m_isOpenSound);

    m_Data->set("opensyssound", m_actSysSound->isChecked());
}

void ToolBar::showMouse()
{
    //数据埋点 - 鼠标录制
    KTrackData *node = kdk_dia_data_init(KEVENTSOURCE_DESKTOP, KEVENT_CLICK);
    kdk_dia_upload_default(node, "enable-showmouse-record", "ToolBar");
    kdk_dia_data_free(node);

    m_isShowMouse = !m_isShowMouse;
    m_actShowMouse->setChecked(m_isShowMouse);

    m_isCursorRec = m_isShowMouse || m_isShowClick || m_isShowPath;
    m_cursorrec->setChecked(m_isCursorRec);

    m_Data->set("showmouse", m_actShowMouse->isChecked());
}

void ToolBar::showMovePath()
{
    KTrackData *node = kdk_dia_data_init(KEVENTSOURCE_DESKTOP, KEVENT_CLICK);
    kdk_dia_upload_default(node, "enable-showmousepath-record", "ToolBar");
    kdk_dia_data_free(node);

    m_isShowPath = !m_isShowPath;
    m_actShowMovePath->setChecked(m_isShowPath);

    m_isCursorRec = m_isShowMouse || m_isShowClick || m_isShowPath;
    m_cursorrec->setChecked(m_isCursorRec);

    m_Data->set("showpath", m_actShowMovePath->isChecked());
}

void ToolBar::clik()
{
    KTrackData *node = kdk_dia_data_init(KEVENTSOURCE_DESKTOP, KEVENT_CLICK);
    kdk_dia_upload_default(node, "enable-showmouseclick-record", "ToolBar");
    kdk_dia_data_free(node);

    m_isShowClick = !m_isShowClick;
    m_actClik->setChecked(m_isShowClick);

    m_isCursorRec = m_isShowMouse || m_isShowClick || m_isShowPath;
    m_cursorrec->setChecked(m_isCursorRec);

    m_Data->set("showclick", m_actClik->isChecked());
}

void ToolBar::keyrecord()
{
    //数据埋点 - 键盘录制
    KTrackData *node = kdk_dia_data_init(KEVENTSOURCE_DESKTOP, KEVENT_CLICK);
    kdk_dia_upload_default(node, "enable-key-record", "ToolBar");
    kdk_dia_data_free(node);

    m_isKeyRec = !m_isKeyRec;
    m_Data->set("keyrecord", m_isKeyRec);
    emit sigShowKey(m_isKeyRec);
}

void ToolBar::initConnect()
{
    connect(m_cursorrec, &KPressButton::clicked, this, &ToolBar::mouserec);
    connect(m_cursorrecNav, &kdk::KBorderlessButton::clicked, this, &ToolBar::mouserecNav);
    connect(m_soundrec, &KPressButton::clicked, this, &ToolBar::soundrec);
    connect(m_soundrecNav, &kdk::KBorderlessButton::clicked, this, &ToolBar::soundrecNav);
    connect(m_keyrec, &KPressButton::clicked, this, &ToolBar::keyrecord);
    connect(m_time, &kdk::KBorderlessButton::clicked, this, &ToolBar::time);
    connect(m_timeNav, &kdk::KBorderlessButton::clicked, this, &ToolBar::timeNav);
    connect(m_setting, &KPressButton::clicked, this, &ToolBar::setting);
    connect(m_close, &KPressButton::clicked, this, &ToolBar::close);
    connect(m_start, &QPushButton::clicked, this, &ToolBar::start);

    connect(m_cursorrecMenu, &QMenu::aboutToHide, [=]() {
        m_cursorrecNav->setIcon(QIcon(":/icon/downarrow.svg"));
    });
    connect(m_soundrecMenu, &QMenu::aboutToHide, [=]() {
        m_soundrecNav->setIcon(QIcon(":/icon/downarrow.svg"));
    });
    connect(m_timeMenu, &QMenu::aboutToHide, [=]() {
        m_timeNav->setIcon(QIcon(":/icon/downarrow.svg"));
    });


    connect(m_actShowMouse, &QAction::triggered, this, &ToolBar::showMouse);
    connect(m_actShowMovePath, &QAction::triggered, this, &ToolBar::showMovePath);
    connect(m_actClik, &QAction::triggered, this, &ToolBar::clik);

    connect(m_actMic, &QAction::triggered, this, &ToolBar::soundMic);
    connect(m_actSysSound, &QAction::triggered, this, &ToolBar::soundSys);

    connect(m_actNone, &QAction::triggered, this, &ToolBar::timeNone);
    connect(m_actThree, &QAction::triggered, this, &ToolBar::timeThree);
    connect(m_actFive, &QAction::triggered, this, &ToolBar::timeFive);
    connect(m_actTen, &QAction::triggered, this, &ToolBar::timeTen);

    connect(m_styleSettings,&QGSettings::changed,this,[=](const QString &key) {
        if (key == "systemFontSize" || key == "systemFont") {
            setFont();
       }
    });
}

void ToolBar::mouserec()
{
    //数据埋点 - 鼠标录制
    KTrackData *node = kdk_dia_data_init(KEVENTSOURCE_DESKTOP, KEVENT_CLICK);
    kdk_dia_upload_default(node, "enable-mouse-record", "ToolBar");
    kdk_dia_data_free(node);

    m_isCursorRec = !m_isCursorRec;
    m_actShowMouse->setChecked(m_isCursorRec);
    m_actShowMovePath->setChecked(m_isCursorRec);
    m_actClik->setChecked(m_isCursorRec);
    m_cursorrec->setChecked(m_isCursorRec);

    m_Data->set("showmouse", m_actShowMouse->isChecked());
    m_Data->set("showpath", m_actShowMovePath->isChecked());
    m_Data->set("showclick", m_actClik->isChecked());
}

void ToolBar::mouserecNav()
{
    m_cursorrecNav->setIcon(QIcon(":/icon/uparrow.svg"));
    QPoint menuPos = m_cursorrecNav->mapToGlobal(QPoint(0, 0)); // 获取m_edit在全局坐标系中的位置
    QSize menuSize = m_cursorrecMenu->sizeHint();
    menuPos.setX(menuPos.x() - ((menuSize.width() - m_cursorrecNav->width()) / 2));
    menuPos.setY(menuPos.y() + 35);
    m_cursorrecMenu->exec(menuPos); // 在计算的位置显示菜单
}

//监听主题
void ToolBar::initGsetting()
{
    connect(kdk::kabase::Gsettings::getPoint(), &kdk::kabase::Gsettings::systemThemeChange, this, [=]() {
        changeStyle();
    });
    changeStyle();
}

void ToolBar::setFont()
{
    QString fontFamily = m_styleSettings->get("systemFont").toString();
    double fontSize = m_styleSettings->get("systemFontSize").toDouble();
    QFont font(fontFamily);
    font.setPointSizeF(fontSize);
    m_cursorrec->setFont(font);
    m_soundrec->setFont(font);
    m_keyrec->setFont(font);
    m_time->setFont(font);
    m_setting->setFont(font);
    m_close->setFont(font);
    m_cursorlabel->setFont(font);
    m_soundlabel->setFont(font);
    m_keylabel->setFont(font);
    m_timelabel->setFont(font);
    m_closelabel->setFont(font);
    m_settinglabel->setFont(font);
    m_startlabel->setFont(font);
    m_cursorlabel->setText(tr("Cursor Recording"));
    m_soundlabel->setText(tr("Sound Recording"));
    m_keylabel->setText(tr("Key Press Recording"));
    m_timelabel->setText(tr("Countdown"));
    m_closelabel->setText(tr("Close"));
    m_closelabel->setAlignment(Qt::AlignCenter);
    m_settinglabel->setText(tr("Setting"));
    m_settinglabel->setAlignment(Qt::AlignCenter);
    m_startlabel->setText(tr("Start"));
    m_startlabel->setAlignment(Qt::AlignCenter);

    longText(m_cursorlabel);
    longText(m_soundlabel);
    longText(m_keylabel);
    longText(m_timelabel);
    QString locale = QLocale::system().name();
    if (fontSize > 11 || locale != "zh_CN") {
        longText(m_closelabel);
        longText(m_settinglabel);
        longText(m_startlabel);
    }

}
void ToolBar::longText(QLabel *nameC)
{
    QFontMetrics fontWidth(nameC->font());
    QString text = nameC->text();
    QString elideNote = fontWidth.elidedText(text, Qt::ElideRight, nameC->width());
    nameC->setText(elideNote);
    if (elideNote.contains("…", Qt::CaseInsensitive)) {
        nameC->setToolTip(text);
    } else {
        nameC->setToolTip("");
    }
}

void ToolBar::changeStyle()
{
    //需要重新梳理此处的QSS
    nowThemeStyle = kdk::kabase::Gsettings::getSystemTheme().toString();
    if ("ukui-dark" == nowThemeStyle || "ukui-black" == nowThemeStyle) {
        this->setStyleSheet("QWidget{border-radius:" + widRadius + ";border:none;color:rgb(255,255,255);}");
        g_tooleWid->setStyleSheet("QWidget{background-color:rgba(0,0,0,0.66);border-radius:" + widRadius
                                  + ";border:none;}");
        m_line1->setStyleSheet("QLabel{border: 1px solid #727272;background-color: #727272; opacity: 1}");

        this->setStyleSheet("QWidget{border-radius:" + widRadius + ";border:none;color:rgb(255,255,255);}");
        g_tooleWid->setStyleSheet("QWidget{background-color:rgba(0,0,0,0.66);border-radius:" + widRadius
                                  + ";border:none;}");
//        m_percentage->setStyleSheet("QLabel{background-color:transparent;color:rgb(255,255,255);}");
    } else {
        this->setStyleSheet("QWidget{border-radius:" + widRadius + ";border:none;color:rgb(0,0,0);}");
        g_tooleWid->setStyleSheet("QWidget{background-color:rgba(255,255,255,1);border:none;border-radius:" + widRadius + ";color:rgb(0,0,0);}");
        m_line1->setStyleSheet("QLabel{border: 1px solid #c5cacb;background-color: #c5cacb; opacity: 1}");
//        m_cursorrec->setStyleSheet("KPressButton{background:rgba(255,255,255,1);font:12pt 微软雅黑 blod;}"
//                      "KPressButton:hover{color:red;}"
//                      "KPressButton:pressed{color:blue}");
//        m_cursorrec->setStyleSheet("KPressButton{background-color:rgba(255,255,255,1); KPressButton:hover{color:rgb(255,0,0);}");//
    }
}

void ToolBar::setBtnSize(QSize acturalSize)
{
    m_cursorrec->setFixedSize(acturalSize);
    m_soundrec->setFixedSize(acturalSize);
    m_keyrec->setFixedSize(acturalSize);
    m_time->setFixedSize(acturalSize);
    m_setting->setFixedSize(acturalSize);
    m_close->setFixedSize(acturalSize);
    m_start->setFixedSize(acturalSize);
}

void ToolBar::setBtnIconSize(QSize iconSize)
{
    m_cursorrec->setIconSize(iconSize);
    m_soundrec->setIconSize(iconSize);
    m_keyrec->setIconSize(iconSize);
    //m_time->setIconSize(iconSize);
    m_setting->setIconSize(iconSize);
    m_close->setIconSize(iconSize);
    m_start->setIconSize(iconSize);
}

void ToolBar::setBtnIcon()
{
    m_cursorrec->setIcon(QIcon(":/icon/cursor.svg"));
    m_cursorrecNav->setIcon(QIcon(":/icon/downarrow.svg"));
    m_soundrec->setIcon(QIcon(":/icon/volume.svg"));
    m_soundrecNav->setIcon(QIcon(":/icon/downarrow.svg"));
    m_keyrec->setIcon(QIcon(":/icon/key.svg"));
    m_timeNav->setIcon(QIcon(":/icon/downarrow.svg"));
    m_setting->setIcon(QIcon(":/icon/setting.svg"));
    m_close->setIcon(QIcon(":/icon/close_rec.svg"));
    m_start->setIcon(QIcon(":/icon/start_rec.svg"));
}

void ToolBar::resumeStatus()
{
    m_Data = new QGSettings(KYLINSCREENCAP);

    m_isShowMouse = m_Data->get("showmouse").toBool();
    m_isShowPath = m_Data->get("showpath").toBool();
    m_isShowClick = m_Data->get("showclick").toBool();
    m_isOpenSysSound = m_Data->get("opensyssound").toBool();
    m_isOpenMic = m_Data->get("openmic").toBool();
    m_isKeyRec = m_Data->get("keyrecord").toBool();
    m_countdown = m_Data->get("cutdown").toInt();

    m_isCursorRec = m_isShowMouse || m_isShowPath || m_isShowClick;
    m_isOpenSound = m_isOpenSysSound || m_isOpenMic;

    m_actShowMouse->setChecked(m_isShowMouse);
    m_actShowMovePath->setChecked(m_isShowPath);
    m_actClik->setChecked(m_isShowClick);
    m_actSysSound->setChecked(m_isOpenSysSound);
    m_actMic->setChecked(m_isOpenMic);
    m_keyrec->setChecked(m_isKeyRec);

    m_cursorrec->setChecked(m_isCursorRec);
    m_soundrec->setChecked(m_isOpenSound);

    if (m_countdown == 0) {
        m_time->setText(tr("none"));

    } else if (m_countdown == 3) {
        m_time->setText(tr("3s"));

    } else if (m_countdown == 5) {
        m_time->setText(tr("5s"));

    } else{
        m_time->setText(tr("10s"));
    }
}

void ToolBar::saveStatus()
{
    m_Data->set("showmouse", m_actShowMouse->isChecked());
    m_Data->set("showpath", m_actShowMovePath->isChecked());
    m_Data->set("showclick", m_actClik->isChecked());

    m_Data->set("opensyssound", m_actSysSound->isChecked());
    m_Data->set("openmic", m_actMic->isChecked());

    m_Data->set("keyrecord", m_keyrec->isChecked());
    m_Data->set("cutdown", m_countdown);
}

void ToolBar::platformType()
{
//    normalIconPath = "sp1";
//    if (platForm.contains(Variable::platForm)) {
        this->resize(TOOL_SIZE);
//        g_tooleWid->resize(530, 80);
        widRadius = "6px";
        this->setBtnSize(TOOL_BUTTON);
//        this->buttonSize(QSize(36, 36));
//    } else {
//        this->resize(TOOLBAR_SIZE);
//        widRadius = "6px";
//        this->buttonSize(TOOL_BUTTON);
//    }
}

void ToolBar::start()
{
    //数据埋点 - 开始录制
    KTrackData *node = kdk_dia_data_init(KEVENTSOURCE_DESKTOP, KEVENT_CLICK);
    kdk_dia_upload_default(node, "start-record", "ToolBar");
    kdk_dia_data_free(node);

    Global::isRecordKey = m_isKeyRec;
    Global::isShowCursor = m_actShowMouse->isChecked();
    Global::isShowCursorPath = m_actShowMovePath->isChecked();
    Global::isShowCursorClick = m_actClik->isChecked();
    Global::isOpenSysSound = m_actSysSound->isChecked();
    Global::isOpenMic = m_actMic->isChecked();
//    saveStatus();

    if(isVisible()){
        emit sigStartRec(m_countdown);
    }
}

void ToolBar::updateMicOption(bool isEnable)
{
    if (isEnable == false && m_actMic->isChecked()){
        soundMic();
    }
    m_actMic->setEnabled(isEnable);
    qDebug()<<"ToolBar::updateMicOption: m_actMic的enable="<<m_actMic->isEnabled();
}

void ToolBar::close()
{
    //数据埋点 - 关闭录制选框GUI
    KTrackData *node = kdk_dia_data_init(KEVENTSOURCE_DESKTOP, KEVENT_CLICK);
    kdk_dia_upload_default(node, "exit-record-GUI", "ToolBar");
    kdk_dia_data_free(node);
    emit sigCloseRecGui();
    Global::recordStatus = false;
}

void ToolBar::setting()
{
    //数据埋点 - 打开设置
    KTrackData *node = kdk_dia_data_init(KEVENTSOURCE_DESKTOP, KEVENT_CLICK);
    kdk_dia_upload_default(node, "open-setting-widow", "ToolBar");
    kdk_dia_data_free(node);

    m_setting->setChecked(false);

    emit sigOpenSettingWid();
}

void ToolBar::soundrec()
{
    //数据埋点 - 声音录制
    KTrackData *node = kdk_dia_data_init(KEVENTSOURCE_DESKTOP, KEVENT_CLICK);
    kdk_dia_upload_default(node, "enable-sound-record", "ToolBar");
    kdk_dia_data_free(node);

    m_isOpenSound = !m_isOpenSound;
    m_isOpenSysSound = m_isOpenSound;
    m_isOpenMic = m_isOpenSound;

    m_actSysSound->setChecked(m_isOpenSysSound);
    if(m_actMic->isEnabled()){
        m_actMic->setChecked(m_isOpenMic);
    }
    m_soundrec->setChecked(m_isOpenSysSound || m_isOpenMic);

    m_Data->set("opensyssound", m_actSysSound->isChecked());
    m_Data->set("openmic", m_actMic->isChecked());
}
